package com.leyou.common.advise;

import com.leyou.common.exception.LyException;
import com.leyou.common.pojo.ExceptionResult;
import lombok.extern.slf4j.Slf4j;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;

@ControllerAdvice//异常集中处理，更好的使业务逻辑与异常处理剥离开
@Slf4j//日志对象
public class BasicExceptionAdvice {
    /*
    handle 操作
    Entity实体
    * */
    @ExceptionHandler(LyException.class)//统一处理某一类异常，（类似aop）从而能够减少代码重复率和复杂度
    public ResponseEntity<ExceptionResult> handleLyException(LyException e) {
        return ResponseEntity.status(e.getStatus()).body(new ExceptionResult(e));
    }

    @ExceptionHandler(RuntimeException.class)
    public ResponseEntity<String> handleRuntimeException(RuntimeException e) {
        return ResponseEntity.status(500).body(e.getMessage());
    }
}
